<?php
/**
 *  商品管理的curd
 * Author: lyun
 * Date: 2018/1/25
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\Admin;

use App\Http\Models\Admin\Product;
use App\Http\Models\Admin\ProductCategory;
use App\Http\Requests\Admin\ProductionDeleteInfoRequest;
use App\Http\Requests\Admin\ProductionIndexRequest;
use App\Http\Requests\Admin\ProductionStoreRequest;
use App\Http\Requests\Admin\ProductionUpdateRequest;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\ApiResponser;
use App\Services\Utils\DeleteRichTextBosKey;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ProductionController extends Controller
{
    use ApiChecker,ApiResponser;

    /**
     *  商品的列表
     *
     * @param ProductionIndexRequest $request
     * @return \Illuminate\Http\Response
     */
    public function index(ProductionIndexRequest $request)
    {
        $products = (new Product)
            ->filterProductCategoryId($request->product_category_id)
            ->filterModelNumber($request->model_number)
            ->filterTitle($request->title)
            ->latest('sort')
            ->paginate($request->per_page?:15);

        return $this->response(compact('products'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = (new ProductCategory)
            ->where('super_id', ProductCategory::TOP_CATEGORY)
            ->with('getSubCategories')
            ->latest('priority')
            ->get();

        $category_types = (new ProductCategory)
            ->where('super_id', '<>', ProductCategory::TOP_CATEGORY)
            ->pluck('name', 'id')
            ->all();

        return $this->response(compact('categories', 'category_types'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  ProductionStoreRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ProductionStoreRequest $request)
    {
        $production_data = $this->transformProductData($request);

        $stored = Product::create($production_data);

        return $this->response(compact('stored'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  ProductionUpdateRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     * @throws \App\Exceptions\AdminManageException
     */
    public function update(ProductionUpdateRequest $request, $id)
    {
        /** @var Product $production */
        $production = (new Product)->find($id);
            $this->checkItemExist($production, '商品');

        $production_data = $this->transformProductData($request);

        $this->updateProductionBosKey($production_data, $production); // 更新富文本中的boskey信息

        $updated = $production->update($production_data);

        return $this->response(compact('updated'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy($id)
    {
        /** @var Product $production */
        $production = (new Product)->find($id);
            $this->checkItemExist($production, '商品');

        // 删除富文本的boskey的值
        (new DeleteRichTextBosKey)->deleteRichTextBosKey($production->detail);
        (new DeleteRichTextBosKey)->deleteRichTextBosKey($production->specification);

        $deleted = $production->delete();

        return $this->response(compact('deleted'));
    }

    /**
     *  删除产品的资料信息
     *
     * @param ProductionDeleteInfoRequest $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function deleteInformation(ProductionDeleteInfoRequest $request)
    {
        /** @var Product $production */
        $production = (new Product)->find($request->id);
            $this->checkItemExist($production, '商品');

        $deleted = $production->deleteInformation();

        $updated = $production->update(['information'=>null]);

        return $this->response(compact('updated'));
    }

    /**
     * @param ProductionStoreRequest|ProductionUpdateRequest|Request $request
     * @return array
     */
    private function transformProductData(Request $request)
    {
        return [
            'product_category_id' => $request->product_category_id,
            'name'                => $request->name?:null,
            'title'               => $request->title,
            'sub_title'           => $request->sub_title?:null,
            'model_number'        => $request->model_number,
            'specification'       => $request->specification,
            'thumb'               => $request->thumb,
            'detail'              => $request->detail,
            'sort'                => $request->sort ?: 0,
            'information'         => $request->information ?: null,
            'keywords'            => $request->keywords,
            'description'         => $request->description,
        ];
    }

    /**
     * @param array $production_data
     * @param Product $production
     */
    private function updateProductionBosKey(array $production_data, Product $production)
    {
        // 更新富文本中的图片，删除原来的boskey
        $new_specification = $production_data['specification'];
        $old_specification = $production->specification;
        (new DeleteRichTextBosKey)->updateRichTextBosKey($new_specification, $old_specification);

        $new_detail = $production_data['detail'];
        $old_detail = $production->detail;
        (new DeleteRichTextBosKey)->updateRichTextBosKey($new_detail, $old_detail);
    }
}
